package com.example.blog.web;

import com.example.blog.pojo.type;
import com.example.blog.service.typeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.javassist.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class typeController {

    @Autowired
    private com.example.blog.service.typeService typeService;

    /**
     *  查询所有type
     *  使用pageHelper进行分页处理
     *  将封装好的分页数据传到model返回前台
     * @param model
     * @param page
     * @return
     */
    @GetMapping({"/admin/types"})
    public String types(Model model, @RequestParam(value = "page" ,defaultValue = "1") Integer page){
        PageHelper.startPage(page,10);
        PageInfo<type> p = new PageInfo<>(typeService.listType());
        model.addAttribute("page",p);
        return "admin/types";
    }

    @GetMapping("/admin/types/input")
    public String adminTypesInput(){

        return "admin/types-input";
    }

    /**
     * type保存
     *  利用是否有传id来判断是编辑还是添加操作
     *  有传id：编辑  没传id：添加
     *  判断名称是否为空或重复，如果为空或重复则把信息传回前台，并拒绝操作
     * @param id
     * @param name
     * @param attributes
     * @return
     * @throws NotFoundException
     */
    @Transactional
    @PostMapping("/admin/types/save")
    public String adminTypeSave(Integer id ,String name, RedirectAttributes attributes) throws NotFoundException {
        if (id==null || id.equals("")){
            if (name == null || name.equals("") ){
                attributes.addFlashAttribute("message","操作失败");
                return "redirect:/admin/types";
            }if (typeService.getType(name) != null){
                attributes.addFlashAttribute("message","分类名称重复");
                return "redirect:/admin/types";
            }else {
                attributes.addFlashAttribute("message","操作成功");
                typeService.saveType(new type(null,name));
                return "redirect:/admin/types";
            }
        }else {
            if (name == null || name.equals("") ){
                attributes.addFlashAttribute("message","操作失败");
                return "redirect:/admin/types";
            }if (typeService.getType(name) != null){
                attributes.addFlashAttribute("message","分类名称重复");
                return "redirect:/admin/types";
            }else {
                attributes.addFlashAttribute("message","操作成功");
                typeService.updateType(id,name);
                return "redirect:/admin/types";
            }

        }

    }

    /**
     * type删除
     *  根据传来的id进行删除操作
     * @param id
     * @return
     */
    @Transactional
    @GetMapping("/admin/types/delete")
    public String adminTypesDelete(Integer id){
        typeService.deleteType(id);
        return "redirect:/admin/types";
    }

}
